﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Asxsyd92Core.Utils;
using Asxsyd92Core.Utils.Data.SQLServer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace WebOSCore.Controllers.Common
{
    public class CommonController : Controller
    {
        /// <summary>
        /// 公共单表保存
        /// </summary>
        /// <param name="tab"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [Route("Common/save")]
        [HttpPost]
        [Authorize]
        public string save(string tab, string data)
        {
            if (tab == null || tab == "") { return JSONhelper.ToJson(new { msg = "tab不能为空！", Success = false }); }
            data = Models.Unity.OperationJson(data, User);
            var id = SqlFromData.SaveFromData(tab, data);
            if (id == Guid.Empty)
            {
                return JSONhelper.ToJson(new { msg = "添加失败！", Success = false });
            }
            else
            {
                return JSONhelper.ToJson(new { msg = "添加成功！", Success = true });
            }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tab">查询哪张表</param>
        /// <param name="title">查询条件</param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        //获取公共list
        [Authorize]
        [HttpGet]
        [Route("Common/GetCommonList")]
        public string GetCommonList(string tab, string title,string desc="ID", int page = 1, int limit = 15)
        {
            IEnumerable<object> o = null;
            if (tab == null || tab == "")
            {
                return Asxsyd92Core.Utils.JSONhelper.ToJson(new { code = 0, msg = "", count = 0, data = o }); ;

            }

            if (title == null || title == "")
            {
                o = Asxsyd92Core.Utils.Data.SQLServer.SqlFromData.GetPage(tab, page, limit, desc);
            }
            else
            {


                o = Asxsyd92Core.Utils.Data.SQLServer.SqlFromData.GetPage(tab, page, limit, desc, new { Name = "%" + title + "%" }, "like");
            }

            if (o.FirstOrDefault() != null)
            {

                var os = Models.Unity.ObjModel(o.FirstOrDefault());
                return Asxsyd92Core.Utils.JSONhelper.ToJson(new { code = 0, msg = "", count = os.FirstOrDefault(c => c.Key.ToString() == "Count").Value, data = o });
            }
            return Asxsyd92Core.Utils.JSONhelper.ToJson(new { code = 0, msg = "", count = 0, data = o });

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        [Route("Common/Del")]
        public string Del(Guid key, string table)
        {
            if (table.IsNullOrEmpty())
                return JSONhelper.ToJson(new { msg = "表数据为空不能删除！", Success = false });
            else
            {
                var tag0 = SqlFromData.DelFromData(table, key);
                //WebOS.Dal.CommonTaskDal.Instance.Delete();
                if (tag0 > 0)
                {
                    return JSONhelper.ToJson(new { msg = "删除成功！", Success = true });
                }
            }
            return JSONhelper.ToJson(new { msg = "删除失败！", Success = false });
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="key"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        [Route("Common/DelList")]
        public string DelList(string key, string table)
        {
            if (table.IsNullOrEmpty())
                return JSONhelper.ToJson(new { msg = "表数据为空不能删除！", Success = false });
            else
            {
                var tag0 = SqlFromData.DelFromData(table, key);
                //WebOS.Dal.CommonTaskDal.Instance.Delete();
                if (tag0 > 0)
                {
                    return JSONhelper.ToJson(new { msg = "删除成功！", Success = true });
                }
            }
            return JSONhelper.ToJson(new { msg = "删除失败！", Success = false });
        }

        /// <summary>
        /// 获取一个学生信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        [Route("Common/GetByID")]
        public string GetByID(Guid key, string table)
        {
            object tag0 = null;
            if (table.IsNullOrEmpty())
                return JSONhelper.ToJson(new { msg = "表数据为空！", Success = false });
            else
            {
                tag0 = SqlFromData.GetFromData(table, key).FirstOrDefault();
                //WebOS.Dal.CommonTaskDal.Instance.Delete();
                if (tag0 != null)
                {
                    return JSONhelper.ToJson(new { data = tag0, Success = true });
                }
            }
            return JSONhelper.ToJson(new { data = tag0, Success = false });
        }

        /// <summary>
        /// 获取一条信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="table"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        [Route("Common/GetByCode")]
        public string GetByCode(string code, string tab)
        {
            object tag0 = null;
            if (tab.IsNullOrEmpty())
                return JSONhelper.ToJson(new { msg = "表数据为空！", Success = false });
            else
            {
                tag0 = WebOS.Dal.DictionaryDal.Instance.GetList("select * from Dictionary where ParentID in (select ID from Dictionary where Code='" + code + "')", null);

                return JSONhelper.ToJson(new { data = tag0, Success = true });

            }

        }


    }
}